package com.magilit.framelibrary.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.magilit.framelibrary.R;
import com.magilit.framelibrary.annotations.ViewInject;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Field;


/**
 * 框架基础类- fragment
 * Created by liuSheng on 2016/11/4.
 */

public abstract class FrameBaseFragment extends Fragment {

    private FrameLayout frameLayout;
    private View contentView;
    private FragmentManager fragmentManager;
    private boolean isRegisterEventBus = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frame_fragment_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        frameLayout = (FrameLayout) view.findViewById(R.id.rootlayout_basefragment);
        frameLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }


    /**
     * 是否显示等待
     *
     * @return true：fragment不会马上显示内容，而是显示loading背景，调用showContent方法显示内容
     * false:直接显示内容
     */
    protected abstract boolean isShowLoading();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        fragmentManager = getFragmentManager();
        onBaseFragmentCreate(savedInstanceState);
    }

    /**
     * Fragment创建
     */
    protected abstract void onBaseFragmentCreate(Bundle savedInstanceState);

    /**
     * 注册EventBus
     */
    protected final void registerEventBus() {
        EventBus.getDefault().register(this);
        isRegisterEventBus = true;
    }


    /**
     * 重写onDestroy，如果注册了EventBus，则需要注销
     */
    @Override
    public void onDestroy() {
        if (isRegisterEventBus) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    protected final FrameLayout getRootLayout() {
        return frameLayout;
    }

    /**
     * 获取Fragment显示的View
     */
    public final View getContentView() {
        return contentView;
    }

    /**
     * 设置内容
     */
    protected final void setMyContentView(int resID) {
        frameLayout.removeAllViews();
        contentView = LayoutInflater.from(getContext()).inflate(resID, frameLayout);
        autoInjectViewField();
    }

    protected final void setMyContentView(View view) {
        contentView = view;
        frameLayout.removeAllViews();
        frameLayout.addView(view);
        autoInjectViewField();
    }

    /**
     * 解析注解，给带有@ViewInject注解的View赋值
     */
    private void autoInjectViewField() {
        try {
            Class<?> clazz = this.getClass();
            Field[] fields = clazz.getDeclaredFields();//获得Fragment中声明的字段
            for (Field field : fields) {
                // 查看这个字段是否有我们自定义的注解类标志的
                if (field.isAnnotationPresent(ViewInject.class)) {
                    ViewInject inject = field.getAnnotation(ViewInject.class);
                    int id = inject.value();
                    if (id > 0) {
                        field.setAccessible(true);
                        field.set(this, frameLayout.findViewById(id));//给我们要找的字段设置值
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    /**
     * 替换该Fragment内部的layout显示为fragment
     */
    protected void replaceChildFragment(int layoutId, FrameBaseFragment fragment, String isBackToStack) {
        FragmentManager childFragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
        if (TextUtils.isEmpty(isBackToStack)) {
            fragmentTransaction.replace(layoutId, fragment);
        } else {
            fragmentTransaction.replace(layoutId, fragment, isBackToStack);
            fragmentTransaction.addToBackStack(isBackToStack);
        }
//        KeyBoardUtils.closeKeyboard(context);
        fragmentTransaction.commitAllowingStateLoss();
    }

    /**
     * 在{@link #setMyContentView(int) or #setMyContentView(View)}之后获取其中View
     */
    protected final View findViewById(int resId) {
        return frameLayout.findViewById(resId);
    }

    protected final View findViewByTag(Object tag) {
        return frameLayout.findViewWithTag(tag);
    }

}